#include<stdio.h>
#include<stdlib.h>

void append(int *a,int n)
{
int i;
char ch;
do
{
a=(int *)realloc(a,(n+1)*sizeof(int));
int z;
printf("Enter a new element:");
scanf("%d",&z);
a[n]=z;
n++;
printf("Do you want to add more items? ");
scanf("%s",&ch);
}while(ch=='Y'||ch=='y');
printf("Elements of the array are:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
//************************************************************************//
void extend(int *a,int n)
{
  int *b,m,j,i;
printf("Enter the number of elements of the other array:");
scanf("%d",&m);
b=malloc(m*sizeof(int));
printf("Enter elements of the other array:");
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
a=(int *)realloc(a,(n+m)*sizeof(int));
for(i=n,j=0;i<(n+m) && j<m;i++,j++)
{
  a[i]=b[j];
}
printf("Elements are:");
for(i=0;i<(n+m);i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
//************************************************************************//
void insert(int *a,int n)
{
  int y,i;
printf("Enter the element to be added: ");
scanf("%d",&y);
int x,*b;
printf("Enter the position: ");
scanf("%d",&x);
a=(int *)realloc(a,(n+1)*sizeof(int));
for(i=n;i>x;i--)
{
  a[i]=a[i-1];
}
a[x]=y;
printf("Elements of the array are:");
for(i=0;i<(n+1);i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
//************************************************************************//
void removee(int *a,int n)
{
int x,i,j,flag=0;
printf("Enter the element you want to delete: ");
scanf("%d",&x);
for(i=0;i<n;i++)
{
  if(a[i]==x)
  {
    flag=1;
    break;
  }
}
if(flag==0)
{
  printf("Error:\n");
  exit(0);
}
a=(int *)realloc(a,(n-1)*sizeof(int));
for(j=(i+1);j<n;j++)
{
  a[j-1]=a[j];
}
printf("Elements are:");
for(i=0;i<(n-1);i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
//************************************************************************//
int pop(int *a,int n)
{
  int x,i;
  char c;
printf("Do you want to enter an index number? ");
scanf(" %c",&c);
if(c=='y'|| c=='Y')
{
  printf("Enter the index number: ");
  scanf("%d",&x);
}
else
{
  x=n-1;
}
printf("Element popped=%d\n",a[x]);
a=(int *)realloc(a,(n-1)*sizeof(int));
for(i=(x+1);i<n;i++)
{
  a[i-1]=a[i];
}
printf("Elements are:");
for(i=0;i<(n-1);i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
//************************************************************************//
void indexx(int *a,int n)
{
  int x,i,flag=0;
printf("Enter the number:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
  if(a[i]==x)
  {
    printf("Number found at position %d\n",i);
    flag=1;
    break;
  }
}
if(flag==0)
{
  printf("Error\n");
}
}
//************************************************************************//
void count(int *a,int n)
{
  printf("Enter the element you want to count:");
int x,i,c=0;
scanf("%d",&x);
for(i=0;i<n;i++)
{
  if(a[i]==x)
  {c++;}
}
printf("Occurence=%d\n",c);
}
//************************************************************************//
void sort(int *a,int n)
{
  int i,j,t;
for(i=0;i<n;i++)
{
  for(j=i;j<n;j++)
  {
    if(a[i]>a[j])
    {
      t=a[i];
      a[i]=a[j];
      a[j]=t;
    }
  }
}
printf("Elements are:");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
//************************************************************************//
void reverse(int *a,int n)
{
int *b,i,j;
b=(int *)malloc(n*sizeof(int));

i = n-1;
for(j=0;j<n;j++,i--)
{
  b[j]=a[i];
}

printf("array:");
i=0;
for(;i<n;i++)
{
printf("%d ",a[i]);
}
printf("Reversed array:");
i=0;
for(;i<n;i++)
{
printf("%d ",b[i]);
}
printf("\n");
}
//************************************************************************//
void main()
{
int *a,n,i,c;
printf("Enter the number of elements:");
scanf("%d",&n);
a=malloc(n*sizeof(int));
printf("Enter elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("MENU:\n1.Append\n2.Extend\n3.Insert\n4.Remove\n5.Pop\n6.Index\n7.Count\n8.Sort\n9.Reverse\n");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
  case 1:
  {
    append(a,n);
    break;
  }
  case 2:
  {
    extend(a,n);
    break;
  }
  case 3:
  {
    insert(a,n);
    break;
  }
  case 4:
  {
    removee(a,n);
    break;
  }
  case 5:
  {
   pop(a,n);
   break;
  }
  case 6:
  {
    indexx(a,n);
    break;
  }
  case 7:
  {
    count(a,n);
    break;
  }
  case 8:
  {
    sort(a,n);
    break;
  }
  case 9:
  {
    reverse(a,n);
    break;
  }
}
}	